package end.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import end.bean.LogInfo;
import end.bean.Mentor;
import end.ser.LogInfoSER;
import end.ser.MentorSER;
import end.ser.imp.MentorSERImp;

/**
 * 
 * MentorForController.java 2016-6-25
 * 
 * Copyright zhaocj Inc. All rights reserved. Love Me Like Love Justin Bieber
 */
@Controller
@RequestMapping("/mentor/")
public class MentorForController {
	@Autowired
	private MentorSER<Mentor> mentorSER;

	public MentorSER<Mentor> getMentorSER() {
		return mentorSER;
	}

	public void setMentorSER(MentorSERImp<Mentor> mentorSER) {
		this.mentorSER = mentorSER;
	}

	@Autowired
	private LogInfoSER<LogInfo> logSER;

	public LogInfoSER<LogInfo> getLogSER() {
		return logSER;
	}

	public void setLogSER(LogInfoSER<LogInfo> logSER) {
		this.logSER = logSER;
	}

	@RequestMapping("queryMentorsByPage")
	public @ResponseBody
	Map<String, Object> queryByPage(Integer page, Integer rows, String sort,
			String order, HttpSession session) throws Exception {
		Mentor mentor = new Mentor();

		mentor.getPageUtil().setPaging(true);
		mentor.getPageUtil().setOrderByCondition(sort);
		mentor.getPageUtil().setQueryCondition(order);
		mentor.getPageUtil().setPageOffset((page - 1) * rows);
		mentor.getPageUtil().setPageSize(rows);
		mentor.setUseStatus('0');

		List<Mentor> beans = mentorSER.selectByBean(mentor);
		Map<String, Object> rets = new HashMap<String, Object>();
		rets.put("total", mentorSER.selectByBeanCount(mentor));
		rets.put("rows", beans);
		return rets;

	}

	@RequestMapping("modify")
	public @ResponseBody
	boolean modifyMentor(Mentor mentor, MultipartFile avatarImg,
			HttpSession session, String avatarChangeFlag) throws Exception {
		ServletContext context = session.getServletContext();
		String realPath = context.getRealPath("/static/file/mentor/avatar");
		if (avatarChangeFlag.equals("ALCHANGE")) {
			if (!avatarImg.isEmpty()) {
				if (!mentor.getAvatar().equals("NOIMAGE")) {
					FileUtils.forceDelete(new File(realPath + "/"
							+ mentor.getAvatar()));
				}
				String suffix = avatarImg.getOriginalFilename().substring(
						avatarImg.getOriginalFilename().lastIndexOf("."));
				avatarImg.transferTo(new File(realPath + "/" + mentor.getId()
						+ suffix));
				mentor.setAvatar(mentor.getId() + suffix);
			} else {
				// 如果没有上传图片 并且之前有图片的话 删除服务器的图片
				if (!mentor.getAvatar().equals("NOIMAGE")) {
					FileUtils.forceDelete(new File(realPath + "/"
							+ mentor.getAvatar()));
					mentor.setAvatar("NOIMAGE");
				}
			}
		}
		mentorSER.updateByPrimaryKey(mentor);
		return true;
	}

	@RequestMapping("add")
	public @ResponseBody
	boolean insertMentor(Mentor mentor, MultipartFile avatarImg,
			HttpSession session) throws Exception {
		mentor.setUseStatus('0');
		mentorSER.insert(mentor);
		ServletContext context = session.getServletContext();
		if (!new File(context.getRealPath("/static/file/mentor/avatar"))
				.exists()) {
			FileUtils.forceMkdir(new File(context
					.getRealPath("/static/file/mentor/avatar")));
		}
		String realPath = context.getRealPath("/static/file/mentor/avatar");
		if (!avatarImg.isEmpty()) {
			String suffix = avatarImg.getOriginalFilename().substring(
					avatarImg.getOriginalFilename().lastIndexOf("."));
			avatarImg.transferTo(new File(realPath + "/" + mentor.getId()
					+ suffix));
			mentor.setAvatar(mentor.getId() + suffix);
		} else {
			mentor.setAvatar("NOIMAGE");
		}
		mentorSER.updateByPrimaryKey(mentor);
		return true;
	}

	@RequestMapping("drop")
	public @ResponseBody
	boolean dropMentors(Integer[] drops) throws Exception {
		List<Integer> dropsArray = new ArrayList<Integer>();
		for (Integer integer : drops) {
			dropsArray.add(integer);
		}
		mentorSER.deleteByPrimaryKey(dropsArray);
		return true;

	}
}
